package com.xmh.reptile.service;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.xmh.reptile.service.NovelService.simpleDateFormat;

public class ReptilePhone {

    private static int count = 0;


    @Test
    public void test4() {
        String charsetName = "UTF-8";
        String startPath = "C:\\Users\\xmh\\Desktop\\phone number\\";
        String endPath = ".txt";

        for (int i = 1; i <= 6; i++) {
            String bookStr = FileUtil.readString(new File(startPath + i + endPath), charsetName);

            List list = JSONObject.parseObject(bookStr, List.class);

            for (Object obj : list) {
                Map<String, Object> map = (Map<String, Object>) obj;

                String nickname = (String) map.get("nickname");

//                if (nickname.contains("(^(\\d{3,4}-)?\\d{7,8})$|(1[3|5|7|8]\\d{9})")) {
//                    System.out.println(nickname);
//                }
                //提取手机号码
                checkCellphone(nickname);
                //提取固定电话号码
                checkTelephone(nickname);
            }
            System.out.println(count);
//            System.out.println(list);
        }
    }

    @Test
    public void test5() {
        String charsetName = "UTF-8";
        String bookStr = FileUtil.readString(new File("C:\\Users\\xmh\\Desktop\\phone number\\发货聊天记录.txt"), charsetName);
        bookStr = bookStr.replace("\r", "");
        String[] split = bookStr.split("]\n");

        for (String json : split) {
            if (StringUtils.isEmpty(json) || StringUtils.isEmpty(json.replace("\n", ""))) {
                continue;
            }

            List list = null;
            try {
                list = JSONObject.parseObject(json + "]", List.class);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            for (Object obj : list) {
                Map<String, Object> map = (Map<String, Object>) obj;

                String nickname = (String) map.get("content");

//                if (nickname.matches("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}")) {
//                    System.out.println(nickname);
//                }
                checkCellphone(nickname);
                checkTelephone(nickname);
            }
        }
    }

    public void main() {
        //要提前号码的字符串
        String str = "n13977777777s18911111111你好15988888888hha0955-7777777sss0775-6678111";
        //提取手机号码
        checkCellphone(str);
        //提取固定电话号码
        checkTelephone(str);
    }

    /**
     * 查询符合的手机号码
     *
     * @param str
     */
    public static void checkCellphone(String str) {
        // 将给定的正则表达式编译到模式中
        Pattern pattern = Pattern.compile("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}");
        // 创建匹配给定输入与此模式的匹配器。
        Matcher matcher = pattern.matcher(str);
        //查找字符串中是否有符合的子字符串
        while (matcher.find()) {
            count++;
            //查找到符合的即输出
            System.out.println(matcher.group() + "\t" + str.replace("\n", ""));
        }
    }

    /**
     * 查询符合的固定电话
     *
     * @param str
     */
    public static void checkTelephone(String str) {
        // 将给定的正则表达式编译到模式中
        Pattern pattern = Pattern.compile("(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)");
        // 创建匹配给定输入与此模式的匹配器。
        Matcher matcher = pattern.matcher(str);
        //查找字符串中是否有符合的子字符串
        while (matcher.find()) {
            //查找到符合的即输出
            System.out.println("查询到一个符合的固定号码：" + matcher.group());
        }
    }

    @Test
    public void test() {
        long date = 1642316314000L;
        System.out.println(simpleDateFormat.format(new Date(date)));

    }

    @Test
    public void test2() throws ParseException {
        Date parse = simpleDateFormat.parse("2020-10-21 13-23-30");

        System.out.println(parse.getTime());
    }
}
